﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFONE
{
    public partial class AddDevice : Form
    {
        public static CDevice cdevice1;
        public AddDevice()
        {
            InitializeComponent();
        }
        private void AddBtn_Click(object sender, EventArgs e)
        {
            //新的设备的名字的命名规则
             string role_of_DeviceName = "设备名称只能包含汉字、字母、数字下划线及空格，且首尾不能包含空格";

            if (!Test(tBDeviceName.Text))
            {
                MessageBox.Show(role_of_DeviceName,"输入的设备名称语法有误");
            }
            else
            {               
               if( CDeviceList.SearchDevice(tBDeviceName.Text) == null)
                {
                    DialogResult dia = MessageBox.Show("确定添加设备" + tBDeviceName.Text + "?", "提示", MessageBoxButtons.OKCancel);
                    if (dia == DialogResult.OK)
                    {
                         cdevice1 = CDeviceList.AddDevice(tBDeviceName.Text);                                               
                        AddDeDataSl cAddDeDataSl = new AddDeDataSl();
                        
                        cAddDeDataSl.Visible = false;
                        cAddDeDataSl.ShowDialog();                                       
                    }                 
                }
                else
                {
                    MessageBox.Show("该设备已经存在，请重新输入");
                }
            }
        }
        bool Test(string input)
        {
            //使用正则表达式判断输入的设备名称是否只包含汉字、字母、数字和下划线
            return System.Text.RegularExpressions.Regex.IsMatch(input, @"^[\w\u4e00-\u9fa5\-_][\s\w\u4e00-\u9fa5]*[\w\u4e00-\u9fa5\-_]$");
        }
       
 
    }
}
